package com.example.swagger.controller;

import com.example.swagger.model.UserSchema;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.*;

/*
 * Copyright (C) 2018 Zhejiang xiaominfo Technology CO.,LTD.
 * All rights reserved.
 * Official Web Site: http://www.xiaominfo.com.
 * Developer Web Site: http://open.xiaominfo.com.
 */

import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;

import java.util.List;

/***
 *
 * @since:knife4j-springdoc-openapi-demo 1.0
 * @author <a href="mailto:xiaoymin@foxmail.com">xiaoymin@foxmail.com</a>
 * 2020/03/15 20:38
 */
 // knife4j 的菜单名称
@Tag(name = "用户接口", description = "the user API")
@RestController
@RequestMapping("/user")
public class UserController {


    @Operation(summary = "Creates list of users with given input array", tags = { "user" })
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "操作成功"),
            @ApiResponse(responseCode = "400", description = "用户提供的无效"),
            @ApiResponse(responseCode = "404", description = "用户不存在")
    })
    @PostMapping(value = "/user/createWithArray", consumes = { "application/json" })
    ResponseEntity<List<UserSchema>> createUsersWithArrayInput(
            @Parameter(description = "List of user object", required = true) @Valid @RequestBody List<UserSchema> user) {
        return ResponseEntity.ok(user);
    }


    @Operation(summary = "普通body请求+Param+Header+Path")
    @Parameters({
            @Parameter(name = "id",description = "文件id",in = ParameterIn.PATH),
            @Parameter(name = "token",description = "请求token",required = true,in = ParameterIn.HEADER),
            @Parameter(name = "name",description = "文件名称",required = true,in=ParameterIn.QUERY)
    })
    @PostMapping("/bodyParamHeaderPath/{id}")
    public ResponseEntity<UserSchema> bodyParamHeaderPath(@PathVariable("id") String id, @RequestHeader("token") String token, @RequestParam("name")String name, @RequestBody UserSchema fileResp){
        fileResp.setName(fileResp.getName()+",receiveName:"+name+",token:"+token+",pathID:"+id);
        return ResponseEntity.ok(fileResp);
    }


    @Operation(summary = "创建用户",description = "根据姓名创建用户")
    @ApiResponse(description = "返回添加的用户", content = @Content(mediaType = "application/json", schema = @Schema(implementation = String.class)))
    @GetMapping("/create")
    public ResponseEntity<String> create(String name){
        return ResponseEntity.ok(name);
    }


    @Operation(summary = "body请求")
    @Parameter(name = "deviceId",description = "设备id",in = ParameterIn.PATH)
    @PostMapping("/{deviceId}/body")
    public ResponseEntity<UserSchema> bodyRequest(@PathVariable("deviceId") String deviceId, @RequestBody Object object){
        System.out.println("deviceId:"+deviceId);
        System.out.println("body:"+object.toString());
        return ResponseEntity.ok(new UserSchema());
    }


    @Operation(summary = "json参数查询1")
    @PostMapping(value = "/getRequest1",produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<UserSchema> getRequest1(@RequestBody UserSchema userSchema, @RequestParam("cade") String cade){
        return ResponseEntity.ok(userSchema);
    }

    @Operation(summary = "Update user", description = "This can only be done by the logged in user.", tags = { "user" })
    @ApiResponses(value = @ApiResponse(description = "successful operation"))
    @PutMapping(value = "/user/{username}", consumes = { "application/json", "application/xml", "application/x-www-form-urlencoded" })
    ResponseEntity<UserSchema> updateUser(
            @Parameter(description = "name that need to be deleted", required = true, schema = @Schema(type = "string")) @PathVariable("username") String username,
            @Parameter(description = "Update an existent user in the store") @Valid @RequestBody UserSchema user) {
        return ResponseEntity.ok(user);
    }


    @Operation(summary = "Get user by user name", tags = { "user" })
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "successful operation", content = { @Content(mediaType = "application/json", schema = @Schema(implementation = UserSchema.class)), @Content(mediaType = "application/xml", schema = @Schema(implementation = UserSchema.class)) }),
            @ApiResponse(responseCode = "400", description = "Invalid username supplied", content = @Content),
            @ApiResponse(responseCode = "404", description = "User not found", content = @Content) })
    @GetMapping(value = "/user/{username}")
    ResponseEntity<String> getUserByName(
            @Parameter(description = "The name that needs to be fetched. Use user1 for testing. ", required = true) @PathVariable("username") String username) {
        return ResponseEntity.ok(username);
    }

}

// knife4j 参考 https://gitee.com/xiaoym/swagger-bootstrap-ui-demo/blob/master/knife4j-spring-boot3-demo/src/main/java/com/github/xiaoymin/boot3/web/UserController.java#
// springdoc-openapi 参考 https://github.com/springdoc/springdoc-openapi-demos/blob/master/demo-spring-boot-3-webmvc/src/main/java/org/springdoc/demo/app2/api/UserApi.java